import axios from "axios";
import {BASE_URL,TIMEOUT } from './config'
import useMainStore from '@/store/modules/mainStore';

const mainStore = useMainStore()

class my_axios{
    constructor(baseURL,timeout=5000){
        this.instance = axios.create({
            baseURL,
            timeout
        })
        this.instance.interceptors.request.use((config)=>{
            // 当数据发起加载的时候显示loading
            mainStore.isLoading = true;
            return config
        })
        this.instance.interceptors.response.use((res)=>{
            mainStore.isLoading = false;
            return res
        },(err)=>{
            mainStore.isLoading = false;
            return err
        })
    }

    request(config){
        
        return new Promise((resolve,reject)=>{
             this.instance.request(config).then(res=>{
                resolve(res.data)
            }).catch(err=>{
                reject(err)
            })
        })
    }
    get(config){
        return this.request({...config,methods:'GET'});
    }
    post(config){
        return this.request({...config,methods:'POST'});
    }
}

export default new my_axios(BASE_URL,TIMEOUT);